package com.frank.service.impl;

import com.frank.dao.JobMapper;
import com.frank.dto.CommonListDto;
import com.frank.dto.JobDto;
import com.frank.dto.QueryPageDto;
import com.frank.model.Job;
import com.frank.model.JobCriteria;
import com.frank.service.RecruitManageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RecruitManageServiceImpl implements RecruitManageService {

    @Resource
    private JobMapper jobMapper;

    @Override
    public CommonListDto getRecruitList(QueryPageDto queryPageDto) {
        int count = jobMapper.selectJobListCount();

        List<Map> mapList = jobMapper.selectJobList(queryPageDto.getMap(), queryPageDto.getRowBounds());

        List<JobDto> jobDtoList = new ArrayList<>();
        for (Map map : mapList){
            JobDto jobDto = new JobDto();
            jobDto.setJobId((Integer)map.get("id"));
            jobDto.setSection((String)map.get("section"));
            jobDto.setJobType((String)map.get("job_type"));
            jobDto.setJobName((String)map.get("job_name"));
            jobDto.setRequireNum((Integer) map.get("require_num"));
            jobDto.setCity((String)map.get("city"));
            jobDto.setRecruitType((String)map.get("recruit_type"));
            jobDto.setDescription((String)map.get("description"));
            jobDto.setRequirement((String)map.get("requirement"));
            jobDto.setRemark((String)map.get("remark"));
            jobDto.setStaffName((String)map.get("staff_name"));
            jobDto.setIsOpen((Integer)map.get("is_open") == 1 ? "是":"否");

            Date endTime = (Date)map.get("end_time");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            jobDto.setEndTime(simpleDateFormat.format(endTime));

            jobDtoList.add(jobDto);
        }

        return new CommonListDto<>(jobDtoList, count);
    }

    @Override
    public List<Job> getJobRecruitListByType(String recruit_type) {
        JobCriteria jobCriteria = new JobCriteria();
        jobCriteria.createCriteria().andRecruitTypeEqualTo(recruit_type).andIsOpenEqualTo(1).andEndTimeGreaterThan(new Date());

        return jobMapper.selectByExample(jobCriteria);
    }

    @Override
    public boolean addRecruitPlan(JobDto jobDto) {
        Job job = new Job();
        job.setSection(jobDto.getSection());
        job.setJobType(jobDto.getJobType());
        job.setJobName(jobDto.getJobName());
        job.setRequireNum(jobDto.getRequireNum());
        job.setCity(jobDto.getCity());
        job.setStaffId(jobDto.getStaffId());
        job.setRecruitType(jobDto.getRecruitType());
        job.setDescription(jobDto.getDescription());
        job.setRequirement(jobDto.getRequirement());
        job.setRemark(jobDto.getRemark());
        job.setIsOpen(jobDto.getIsOpen().equals("是") ? 1 : 0);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            job.setEndTime(simpleDateFormat.parse(jobDto.getEndTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return jobMapper.insertSelective(job) == 1;
    }

    @Override
    public boolean modifyRecruit(JobDto jobDto) {
        Job job = new Job();
        job.setId(jobDto.getJobId());
        job.setSection(jobDto.getSection());
        job.setJobType(jobDto.getJobType());
        job.setJobName(jobDto.getJobName());
        job.setRequireNum(jobDto.getRequireNum());
        job.setCity(jobDto.getCity());
        job.setRecruitType(jobDto.getRecruitType());
        job.setDescription(jobDto.getDescription());
        job.setRequirement(jobDto.getRequirement());
        job.setRemark(jobDto.getRemark());
        job.setIsOpen(jobDto.getIsOpen().equals("是") ? 1 : 0);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            job.setEndTime(simpleDateFormat.parse(jobDto.getEndTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return jobMapper.updateByPrimaryKeySelective(job) == 1;
    }

    @Override
    public boolean deleteRecruit(Integer jobId) {
        return jobMapper.deleteByPrimaryKey(jobId) == 1;
    }
}
